home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / ode-initval / gear1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  3.5 KB  |  155 lines

  1. /* ode-initval/gear1.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Gear 1 */
  21.  
  22. /* Author:  G. Jungman
  23.  */
  24. #include <config.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <gsl/gsl_math.h>
  28. #include <gsl/gsl_errno.h>
  29. #include <gsl/gsl_odeiv.h>
  30.  
  31. #include "odeiv_util.h"
  32.  
  33. typedef struct
  34. {
  35.   double *k;
  36.   double *y0;
  37. }
  38. gear1_state_t;
  39.  
  40. static void *
  41. gear1_alloc (size_t dim)
  42. {
  43.   gear1_state_t *state = (gear1_state_t *) malloc (sizeof (gear1_state_t));
  44.  
  45.   if (state == 0)
  46.     {
  47.       GSL_ERROR_NULL ("failed to allocate space for gear1_state", GSL_ENOMEM);
  48.     }
  49.  
  50.   state->k = (double *) malloc (dim * sizeof (double));
  51.  
  52.   if (state->k == 0)
  53.     {
  54.       free (state);
  55.       GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM);
  56.     }
  57.  
  58.   state->y0 = (double *) malloc (dim * sizeof (double));
  59.  
  60.   if (state->y0 == 0)
  61.     {
  62.       free (state->k);
  63.       free (state);
  64.       GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
  65.     }
  66.  
  67.   return state;
  68. }
  69.  
  70. static int
  71. gear1_apply(void * vstate,
  72.             size_t dim,
  73.             double t,
  74.             double h,
  75.             double y[],
  76.             double yerr[],
  77.             const double dydt_in[],
  78.             double dydt_out[],
  79.             const gsl_odeiv_system * sys)
  80. {
  81.   gear1_state_t *state = (gear1_state_t *) vstate;
  82.  
  83.   const int iter_steps = 3;
  84.   int status = 0;
  85.   int nu;
  86.   size_t i;
  87.  
  88.   double * const k = state->k;
  89.   double * const y0 = state->y0;
  90.  
  91.   DISCARD_POINTER(dydt_in); /* prevent warning about unused parameter */
  92.  
  93.   DBL_MEMCPY(y0, y, dim);
  94.  
  95.   /* iterative solution */
  96.   for(nu=0; nu<iter_steps; nu++) {
  97.     int s = GSL_ODEIV_FN_EVAL(sys, t + h, y, k);
  98.     GSL_STATUS_UPDATE(&status, s);
  99.     for(i=0; i<dim; i++) {
  100.       y[i] = y0[i] + h * k[i];
  101.     }
  102.   }
  103.  
  104.   /* fudge the error estimate */
  105.   for(i=0; i<dim; i++) {
  106.     yerr[i] = h * h * k[i];
  107.   }
  108.  
  109.   if(dydt_out != NULL) {
  110.     DBL_MEMCPY(dydt_out, k, dim);
  111.   }
  112.  
  113.   return status;
  114. }
  115.  
  116. static int
  117. gear1_reset (void *vstate, size_t dim)
  118. {
  119.   gear1_state_t *state = (gear1_state_t *) vstate;
  120.  
  121.   DBL_ZERO_MEMSET (state->k, dim);
  122.   DBL_ZERO_MEMSET (state->y0, dim);
  123.  
  124.   return GSL_SUCCESS;
  125. }
  126.  
  127. static unsigned int
  128. gear1_order (void *vstate)
  129. {
  130.   gear1_state_t *state = (gear1_state_t *) vstate;
  131.   state = 0; /* prevent warnings about unused parameters */
  132.   return 2;
  133. }
  134.  
  135. static void
  136. gear1_free (void *vstate)
  137. {
  138.   gear1_state_t *state = (gear1_state_t *) vstate;
  139.   free (state->k);
  140.   free (state->y0);
  141.   free (state);
  142. }
  143.  
  144. static const gsl_odeiv_step_type gear1_type = { "gear1",    /* name */
  145.   1,                /* can use dydt_in */
  146.   0,                /* gives exact dydt_out */
  147.   &gear1_alloc,
  148.   &gear1_apply,
  149.   &gear1_reset,
  150.   &gear1_order,
  151.   &gear1_free
  152. };
  153.  
  154. const gsl_odeiv_step_type *gsl_odeiv_step_gear1 = &gear1_type;
  155.